﻿// -----------------------------------------------------------------------
// <copyright file="ProfileTypeLegalGroundsListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db ProfileTypeLegalGroundsList
    /// </summary>
    public class ProfileTypeLegalGroundsListData
    {
        /// <summary>
        /// Get ProfileTypeLegalGroundsList By Id
        /// </summary>
        /// <param name="profileTypeLegalGroundsListId">Param profileTypeLegalGroundsListId</param>
        /// <returns>Return ProfileTypeLegalGroundsList</returns>
        public ProfileTypeLegalGroundsListEN GetProfileTypeLegalGroundsListById(int profileTypeLegalGroundsListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ProfileTypeLegalGroundsListEN profileTypeLegalGroundsList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeLegalGroundsListGetById, profileTypeLegalGroundsListId);
                while (reader.Read())
                {
                    profileTypeLegalGroundsList = new ProfileTypeLegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeLegalGroundsList.ProfileTypeLegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeLegalGroundsList.ProfileTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeLegalGroundsList.LegalGroundsListId = reader.GetInt32(2);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return profileTypeLegalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ProfileTypeLegalGroundsList
        /// </summary>
        /// <returns>Return list all ProfileTypeLegalGroundsList</returns>
        public IList<ProfileTypeLegalGroundsListEN> GetProfileTypeLegalGroundsLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeLegalGroundsListEN> lstProfileTypeLegalGroundsList = new List<ProfileTypeLegalGroundsListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeLegalGroundsListGetAll);
                while (reader.Read())
                {
                    ProfileTypeLegalGroundsListEN profileTypeLegalGroundsList = new ProfileTypeLegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeLegalGroundsList.ProfileTypeLegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeLegalGroundsList.ProfileTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeLegalGroundsList.LegalGroundsListId = reader.GetInt32(2);
                    }

                    lstProfileTypeLegalGroundsList.Add(profileTypeLegalGroundsList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeLegalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ProfileTypeLegalGroundsList
        /// </summary>
        /// <param name="profileTypeLegalGroundsList">Paarm ProfileTypeLegalGroundsList</param>
        /// <returns>Insert ProfileTypeLegalGroundsList</returns>
        public int Insert(ProfileTypeLegalGroundsListEN profileTypeLegalGroundsList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeLegalGroundsList.ProfileTypeId, profileTypeLegalGroundsList.ProfileTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeLegalGroundsList.LegalGroundsListId, profileTypeLegalGroundsList.LegalGroundsListId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeLegalGroundsListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ProfileTypeLegalGroundsList
        /// </summary>
        /// <param name="profileTypeLegalGroundsList">Param ProfileTypeLegalGroundsList</param>
        public void Update(ProfileTypeLegalGroundsListEN profileTypeLegalGroundsList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeLegalGroundsList.ProfileTypeLegalGroundsListId, profileTypeLegalGroundsList.ProfileTypeLegalGroundsListId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeLegalGroundsList.ProfileTypeId, profileTypeLegalGroundsList.ProfileTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeLegalGroundsList.LegalGroundsListId, profileTypeLegalGroundsList.LegalGroundsListId)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeLegalGroundsListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ProfileTypeLegalGroundsList by profileTypeLegalGroundsListId
        /// </summary>
        /// <param name="profileTypeLegalGroundsListId">param profileTypeLegalGroundsListId</param>
        public void Delete(int profileTypeLegalGroundsListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeLegalGroundsList.ProfileTypeLegalGroundsListId, profileTypeLegalGroundsListId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeLegalGroundsListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List ProfileTypeLegalGroundsList By ProfileTypeId
        /// </summary>
        /// <param name="profileTypeId">Param profileTypeId</param>
        /// <returns>Return List ProfileTypeLegalGroundsList</returns>
        public IList<ProfileTypeLegalGroundsListEN> GetProfileTypeLegalGroundsListByProfileTypeId(int profileTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeLegalGroundsListEN> lstProfileTypeLegalGroundsList = new List<ProfileTypeLegalGroundsListEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeLegalGroundsListGetByProfileTypeId, profileTypeId);
                while (reader.Read())
                {
                    ProfileTypeLegalGroundsListEN profileTypeLegalGroundsList = new ProfileTypeLegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeLegalGroundsList.ProfileTypeLegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeLegalGroundsList.ProfileTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeLegalGroundsList.LegalGroundsListId = reader.GetInt32(2);
                    }

                    lstProfileTypeLegalGroundsList.Add(profileTypeLegalGroundsList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeLegalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List ProfileTypeLegalGroundsList By LegalGroundsListId
        /// </summary>
        /// <param name="legalGroundsListId">Param legalGroundsListId</param>
        /// <returns>Return List ProfileTypeLegalGroundsList</returns>
        public IList<ProfileTypeLegalGroundsListEN> GetProfileTypeLegalGroundsListByLegalGroundsListId(int legalGroundsListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeLegalGroundsListEN> lstProfileTypeLegalGroundsList = new List<ProfileTypeLegalGroundsListEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeLegalGroundsListGetByLegalGroundsListId, legalGroundsListId);
                while (reader.Read())
                {
                    ProfileTypeLegalGroundsListEN profileTypeLegalGroundsList = new ProfileTypeLegalGroundsListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeLegalGroundsList.ProfileTypeLegalGroundsListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeLegalGroundsList.ProfileTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeLegalGroundsList.LegalGroundsListId = reader.GetInt32(2);
                    }

                    lstProfileTypeLegalGroundsList.Add(profileTypeLegalGroundsList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeLegalGroundsList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}